﻿using System;
using System.Timers;
using System.Windows.Forms;

namespace APP
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }
        //https://weixin.sogou.com/weixin?query=%E5%A4%A7%E6%95%B0%E6%8D%AE%E6%96%87%E6%91%98
        private void btn_begin_Click(object sender, EventArgs e)
        {
            
            //
            var value = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["time"]);
            System.Timers.Timer timer = new System.Timers.Timer();
            //第一次启动
            Query(null,null);
            timer.Enabled = true;

            timer.Interval = value*60000; 

            timer.Start();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(Query);
            //if ()
            //{
            //    MessageBox.Show("下载完成！");
            //}
            //else
            //{
            //    MessageBox.Show("下载异常");
            //}
        }
        private int number = 1;

        private void Query(object sender, ElapsedEventArgs e)
        {
            textBox1.Invoke(new Action(() => {
                textBox1.AppendText("开始执行第：" + number);
                textBox1.AppendText("\r\n");
            }));
            try
            {
                var isSuccess = WeiXinNewsCrawler.Query(textBox_Url.Text);
                textBox1.Invoke(new Action(() => {
                    textBox1.AppendText("第" + number + "次执行结束" + isSuccess);
                    textBox1.AppendText("\r\n");
                }));
            }
            catch (Exception ex)
            {
                textBox1.Invoke(new Action(() => {
                    textBox1.AppendText("第" + number + "次执行结束,执行失败" + ex.Message);
                    textBox1.AppendText("\r\n");
                }));
            }
            
            
            number++;
        }
    }

   
}
